import store from '@/store/index.js'
import router from './index'

const pageList = ['/setting', '/profile', '/user']
router.beforeEach(function (to, form, next) {
  if (!store.state.tokenInfo.token) {
    if (pageList.includes(to.path)) {
      // fullPath解析完整的路径 encode 防止参数转换为query对象
      next('/login?backto=' + encodeURIComponent(to.fullPath))
    } else {
      next()
    }
  } else {
    // 已经登陆，就不能访问login,
    if (to.path === '/login') {
      console.log('已经登陆，就不能访问login,回去主页')
      next('/')
    } else {
      next()
    }
  }
})

export default router
